//
//  ViewController.m
//  Network
//
//  Created by 韩艳锋 on 2018/5/3.
//  Copyright © 2018年 韩艳锋. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController{
    
    UIImageView * imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:imageView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSURL * url = [[NSURL alloc]initWithString:@"http://169.254.46.90/artistsbac.mp3"];
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
    [request addValue:@"Mon, 23 Apr 2018 06:52:12 GMT" forHTTPHeaderField:@"If-Modified-Since"];
    NSURLSessionTask * task;
    task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSHTTPURLResponse * httpresponse = (NSHTTPURLResponse *)response;
        NSLog(@"%@/n%@", httpresponse, request);
        
        if (data != nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
                imageView.image = [UIImage imageWithData:data];
            });
        }
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"currentRequest\n%@",[[task currentRequest] allHTTPHeaderFields]);
        NSLog(@"originalRequest\n%@",[[task originalRequest] allHTTPHeaderFields]);
    });
    [task resume];
}
//sudo apachectl start/restar
@end
